package org.vitaliyl.monitoring.server.beans;

import java.util.Date;

import org.vitaliyl.monitoring.server.Datastore;

import com.google.appengine.api.datastore.Entity;

public class MessageDescriptor {
	public static final String CONNECTIONS_INFO_FORMAT = "%d/%d";
	private String serverUrl;
	private int maxConnections;
	private int currentConnections;
	private boolean isDown;
	private Date when;

	public Date getWhen() {
		return when;
	}

	public void setWhen(Date when) {
		this.when = when;
	}

	public MessageDescriptor(ServerDescriptor sd) {
		this.maxConnections = sd.getMaxConnections();
		this.serverUrl = sd.getServerUrl();
	}

	public MessageDescriptor(String serverUrl, int maxConnections, int currentConnections, boolean isDown) {
		this.currentConnections = currentConnections;
		this.maxConnections = maxConnections;
		this.serverUrl = serverUrl;
		this.isDown = isDown;
	}

	public boolean isDown() {
		return isDown;
	}

	public void setDown(boolean isDown) {
		this.isDown = isDown;
	}

	public String getServerUrl() {
		return serverUrl;
	}

	public void setServerUrl(String serverUrl) {
		this.serverUrl = serverUrl;
	}

	public int getCurrentConnections() {
		return currentConnections;
	}

	public void setCurrentConnections(int currentConnections) {
		this.currentConnections = currentConnections;
	}

	public int getMaxConnections() {
		return maxConnections;
	}

	public void setMaxConnections(int maxConnections) {
		this.maxConnections = maxConnections;
	}

	public String getConnectionsInfo() {
		return String.format(CONNECTIONS_INFO_FORMAT, currentConnections, maxConnections);
	}

	@Override
	public String toString() {
		if (isDown) {
			return "Server is down";
		}
		if (currentConnections > maxConnections) {
			return "Connection limit exceeded";
		}
		return super.toString();
	}

	public boolean hasProblem() {
		return isDown || (currentConnections > maxConnections);
	}

	public static Entity toEntity(MessageDescriptor descriptor) {
		Entity entity = new Entity(Datastore.MESSAGE_TYPE);
		entity.setProperty(Datastore.SERVER_IS_DOWN, descriptor.isDown());
		entity.setProperty(Datastore.SERVER_CONNECTIONS, descriptor.getCurrentConnections());
		entity.setProperty(Datastore.SERVER_MAX_CONNECTIONS, descriptor.getMaxConnections());
		entity.setProperty(Datastore.SERVER_SERVER_URL, descriptor.getServerUrl());
		entity.setProperty(Datastore.SERVER_PROBLEM_TIME, descriptor.getWhen()
				.getTime());
		return entity;
	}

	public static MessageDescriptor fromEntity(Entity entity) {
		String serverUrl = (String) entity.getProperty(Datastore.SERVER_SERVER_URL);
		int maxConnections = ((Long) entity.getProperty(Datastore.SERVER_MAX_CONNECTIONS)).intValue();
		int currentConnections = ((Long) entity.getProperty(Datastore.SERVER_CONNECTIONS)).intValue();
		boolean isDown = (Boolean) entity.getProperty(Datastore.SERVER_IS_DOWN);
		Long when = (Long) entity.getProperty(Datastore.SERVER_PROBLEM_TIME);
	
		MessageDescriptor descriptor = new MessageDescriptor(serverUrl, maxConnections, currentConnections, isDown);
		if (when != null) {
			descriptor.setWhen(new Date(when));
		}
		return descriptor;
	}
}
